package com.ych.modules.api.authorInfc.fileAuth.controller;

import com.ych.modules.api.authorInfc.fileAuth.service.IFileService;
import com.ych.utils.TLMap;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/11
 * Time: 13:49
 * Description:
 */
@Api(tags = {"文件上传"})
@RestController
@RequestMapping("/api/auth/uploadFile")
public class FileController {

    @Autowired
    private IFileService iFileService;

    @ApiOperation(value = "获取文件上传流", notes = "获取文件流", position = 1)
    @RequestMapping(value = "/upload", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    public R<String> uploadFile(
            @ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) {
        try {
            String url = iFileService.uploadFile(file, TLMap.getUserId());
            return new R(0, "上传成功", url);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        } catch (Exception e) {
            return new R(2, "系统未知异常" + e.getMessage(), null);
        }
    }
}
